package com.s7.recycler.holder;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administor on 2018\7\11 0011.
 */
public abstract class BaseHolder extends RecyclerView.ViewHolder {

    private Map<Integer, View> mViewMap;

    public BaseHolder(View itemView) {
        super(itemView);
        mViewMap = new HashMap<>();
    }

    public View getItemView() {
        return itemView;
    }

    /**
     * 获取设置的view
     * @param id
     * @return
     */
    public <T extends View> T findViewById(int id) {
        View view = mViewMap.get(id);
        if (view == null) {
            view = itemView.findViewById(id);
            mViewMap.put(id, view);
        }
        return (T) view;
    }

    /**
     * 获取 View 控件
     * @param id
     * @return
     */
    public View getView(int id) {
        return findViewById(id);
    }

    /**
     * 获取 TextView 控件
     * @param id
     * @return
     */
    public TextView getTextView(int id) {
        return (TextView) getView(id);
    }

    /**
     * 获取 Button 控件
     * @param id
     * @return
     */
    public Button getButton(int id) {
        return (Button) getView(id);
    }

    /**
     * 获取 ImageView 控件
     * @param id
     * @return
     */
    public ImageView getImageView(int id) {
        return (ImageView) getView(id);
    }

    /**
     * 获取 ImageButton 控件
     * @param id
     * @return
     */
    public ImageButton getImageButton(int id) {
        return (ImageButton) getView(id);
    }

    /**
     * 获取 EditText 控件
     * @param id
     * @return
     */
    public EditText getEditText(int id) {
        return (EditText) getView(id);
    }

    /**
     * 获取 RatingBar 控件
     * @param id
     * @return
     */
    public RatingBar getRatingBar(int id) {
        return (RatingBar) getView(id);
    }

    /**
     * 获取上下文对象
     * @return
     */
    public final Context getContext() {
        return itemView.getContext();
    }

    /**
     * 在Adapter中 对应位置
     */
    private int realPosition;

    /**
     * 设置对应位置
     * @param realPosition
     */
    public void setRealPosition(int realPosition) {
        this.realPosition = realPosition;
    }

    /**
     * 获取对应位置
     * @return
     */
    public int getRealPosition() {
        return realPosition;
    }

    /**
     * 设置控件宽高
     * @param id
     * @param width
     * @param height
     */
    public void setLayoutParams(@IdRes int id, int width, int height) {
        findViewById(id).getLayoutParams().width = width;
        findViewById(id).getLayoutParams().height = height;
    }

    /**
     * 设置背景
     * @param id
     * @param color
     */
    public void setBackgroundColor(@IdRes int id, @ColorInt int color) {
        findViewById(id).setBackgroundColor(color);
    }

    /**
     * 设置背景
     * @param id
     * @param resId
     */
    public void setBackgroundResource(@IdRes int id, @DrawableRes int resId) {
        findViewById(id).setBackgroundResource(resId);
    }

    /**
     * 设置背景
     * @param id
     * @param drawable
     */
    public void setBackgroundDrawable(@IdRes int id, Drawable drawable) {
        findViewById(id).setBackground(drawable);
    }

    /**
     * 设置tag
     * @param id
     * @param tag
     */
    public void setTag(@IdRes int id, Object tag) {
        findViewById(id).setTag(tag);
    }

    /**
     * 设置图片
     * @param id
     * @param drawable
     */
    public void setImageDrawable(@IdRes int id, @Nullable Drawable drawable) {
        View view = findViewById(id);
        if (view instanceof ImageView) {
            ((ImageView) view).setImageDrawable(drawable);
        }
    }

    /**
     * 设置图片
     * @param id
     * @param image
     */
    public void setImageResource(@IdRes int id, @DrawableRes int image) {
        View view = findViewById(id);
        if (view instanceof ImageView) {
            ((ImageView) view).setImageResource(image);
        }
    }

    /**
     * 设置图片的等级
     * @param id
     * @param level
     * @return
     */
    public void setImageLevel(@IdRes int id, int level) {
        View view = findViewById(id);
        if (view instanceof ImageView) {
            ((ImageView) view).setImageLevel(level);
        }
    }

    /**
     * 给图片着色
     * @param id
     * @param tint 颜色
     * @return
     */
    public void setImageTint(@IdRes int id, ColorStateList tint) {
        View view = findViewById(id);
        if (view instanceof ImageView) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ((ImageView) view).setImageTintList(tint);
            }
        }
    }

    /**
     * TextView 设置文字
     * @param id
     * @param text
     */
    public void setText(@IdRes int id, @StringRes int text) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        }
    }

    /**
     * TextView 设置文字
     * @param id
     * @param text
     */
    public void setText(@IdRes int id, String text) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        }
    }

    /**
     * TextView 设置文字
     * @param id
     * @param text
     */
    public void setText(@IdRes int id, CharSequence text) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        }
    }


    /**
     * TextView 设置图片
     * @param id
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setTextDrawable(@IdRes int id, @Nullable Drawable left, @Nullable Drawable top,
                             @Nullable Drawable right, @Nullable Drawable bottom) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setCompoundDrawables(left, top, right, bottom);
        }
    }

    /**
     * TextView 设置图片
     * @param id
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @param padding
     */
    public void setTextDrawable(@IdRes int id, @Nullable Drawable left, @Nullable Drawable top,
                             @Nullable Drawable right, @Nullable Drawable bottom, int padding) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setCompoundDrawables(left, top, right, bottom);
            ((TextView) view).setCompoundDrawablePadding(padding);
        }
    }

    /**
     * TextView 设置颜色
     * @param id
     * @param color
     */
    public void setTextColor(@IdRes int id, @ColorInt int color) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(color);
        }
    }

    /**
     * TextView 设置颜色
     * @param id
     * @param color
     */
    public void setTextColor(@IdRes int id, String color) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(Color.parseColor(color));
        }
    }

    /**
     * TextView 设置颜色
     * @param id
     * @param color
     */
    public void setTextColorResources(@IdRes int id, @ColorRes int color) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(ContextCompat.getColor(view.getContext(), color));
        }
    }

    /**
     * 设置文字变化监听
     * @param id
     * @param watcher
     * @return
     */
    public void setTextWatcher(@IdRes int id, TextWatcher watcher) {
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).addTextChangedListener(watcher);
        }
    }

    /**
     * Button 设置文字
     * @param id
     * @param text
     */
    public void setButton(@IdRes int id, @StringRes int text) {
        View view = findViewById(id);
        if (view instanceof Button) {
            ((Button) view).setText(text);
        }
    }

    /**
     * Button 设置文字
     * @param id
     * @param text
     */
    public void setButton(@IdRes int id, String text) {
        View view = findViewById(id);
        if (view instanceof Button) {
            ((Button) view).setText(text);
        }
    }

    /**
     * Button 设置文字
     * @param id
     * @param text
     */
    public void setButton(@IdRes int id, CharSequence text) {
        View view = findViewById(id);
        if (view instanceof Button) {
            ((Button) view).setText(text);
        }
    }

    /**
     * Button 设置颜色
     * @param id
     * @param color
     */
    public void setButtonColor(@IdRes int id, @ColorInt int color) {
        View view = findViewById(id);
        if (view instanceof Button) {
            ((Button) view).setTextColor(color);
        }
    }

    /**
     * Button 设置颜色
     * @param id
     * @param color
     */
    public void setButtonColor(@IdRes int id, String color) {
        View view = findViewById(id);
        if (view instanceof Button) {
            ((Button) view).setTextColor(Color.parseColor(color));
        }
    }

    /**
     * Button 设置颜色
     * @param id
     * @param color
     */
    public void setButtonColorResources(@IdRes int id, @ColorRes int color) {
        View view = findViewById(id);
        if (view instanceof Button) {
            ((Button) view).setTextColor(ContextCompat.getColor(view.getContext(), color));
        }
    }

    /**
     * 设置输入框是否可编辑
     * @param id
     * @param enable
     * @return
     */
    public void setEditEnable(@IdRes int id, boolean enable) {
        View view = findViewById(id);
        view.setEnabled(enable);
        if (view instanceof EditText) {
            view.setFocusable(enable);
            view.setFocusableInTouchMode(enable);
        }
    }

    /**
     * 设置控件是否显示
     * @param id
     * @param visibility
     */
    public void setVisibility(@IdRes int id, int visibility) {
        getView(id).setVisibility(visibility);
    }

    /**
     * CheckBox 设置选中状态
     * @param id
     * @param checked
     */
    public void setChecked(@IdRes int id, boolean checked) {
        View view = findViewById(id);
        if (view instanceof CompoundButton) {
            ((CompoundButton) view).setChecked(checked);
        }
    }

    /**
     * RatingBar 设置设置分级
     * @param id
     * @param rating
     */
    public void setRating(@IdRes int id, float rating) {
        View view = findViewById(id);
        if (view instanceof RatingBar) {
            ((RatingBar) view).setRating(rating);
        }
    }

}
